package com.xxwy.ddu.security.code.authonrize.impl;

import com.xxwy.ddu.security.code.authonrize.AuthorizeConfigProvider;
import com.xxwy.ddu.security.code.properties.SecurityConstants;
import com.xxwy.ddu.security.code.properties.SecurityProperties;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.stereotype.Component;

/**
 * @author xxwy
 * on 2018/8/24 0024
 * 每个示例中可以有自己的权限Provider
 */
@Component
@Order(Integer.MIN_VALUE)
public class InitAuthorizeAuthorizeConfigProvider implements AuthorizeConfigProvider {

    @Autowired
    private SecurityProperties securityProperties;


    @Override
    public boolean config(ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry config) {
        config.antMatchers(SecurityConstants.DEFAULT_UNAUTHENTICATION_URL,
                securityProperties.getBrowser().getSignUpUrl(),
                securityProperties.getBrowser().getSignInUrl(),
                securityProperties.getSocial().getFilterProcessUrl(),
                SecurityConstants.DEFAULT_USER,
                SecurityConstants.DEFAULT_LOGIN_PROCESSING_URL_MOBILE,
                SecurityConstants.DEFAULT_VALIDATE_CODE_URL_PREFIX + "/*",
                securityProperties.getBrowser().getSession().getSessionInvalidUrl())
                .permitAll();
        if (StringUtils.isNotBlank(securityProperties.getBrowser().getSignOutUrl())) {
            config.antMatchers(securityProperties.getBrowser().getSignOutUrl()).permitAll();
        }
        return false;
    }
}
